package com.muyu.danmo.controller;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoInformationBasic;
import com.muyu.danmo.service.IDanmoInformationBasicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基本信息Controller
 * 
 * @author yangle
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/danmoInformationBasic")
public class DanmoInformationBasicController extends BaseController
{
    @Autowired
    private IDanmoInformationBasicService danmoInformationBasicService;

    /**
     * 查询基本信息列表
     */
//    @RequiresPermissions("system:danmoInformationBasic:list")
    @GetMapping("/list")
    public Result list(DanmoInformationBasic danmoInformationBasic)
    {

        List<DanmoInformationBasic> list = danmoInformationBasicService.selectDanmoInformationBasicList(danmoInformationBasic);
        return Result.success(list);
    }

    /**
     * 导出基本信息列表
     */
//    @RequiresPermissions("system:danmoInformationBasic:export")
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoInformationBasic danmoInformationBasic)
    {
        List<DanmoInformationBasic> list = danmoInformationBasicService.selectDanmoInformationBasicList(danmoInformationBasic);
        ExcelUtil<DanmoInformationBasic> util = new ExcelUtil<DanmoInformationBasic>(DanmoInformationBasic.class);
        util.exportExcel(response, list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
//    @RequiresPermissions("system:danmoInformationBasic:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(danmoInformationBasicService.selectDanmoInformationBasicById(id));
    }

    /**
     * 新增基本信息
     */
//    @RequiresPermissions("system:danmoInformationBasic:add")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result add(@RequestBody DanmoInformationBasic danmoInformationBasic)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        danmoInformationBasic.setUserId(loginUser.getUserid());
        DanmoInformationBasic info = danmoInformationBasicService.getInfo(loginUser.getUserid());
        if (info!=null){
            danmoInformationBasic.setId(info.getId());

            return   danmoInformationBasicService.updateDanmoInformationBasic(danmoInformationBasic);
        }else {
            return danmoInformationBasicService.insertDanmoInformationBasic(danmoInformationBasic);
        }
    }

    /**
     * 修改基本信息
     */
//    @RequiresPermissions("system:danmoInformationBasic:edit")
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody DanmoInformationBasic danmoInformationBasic)
    {
        return danmoInformationBasicService.updateDanmoInformationBasic(danmoInformationBasic);
    }

    /**
     * 删除基本信息
     */
//    @RequiresPermissions("system:danmoInformationBasic:remove")
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return danmoInformationBasicService.deleteDanmoInformationBasicByIds(ids);
    }

    /**  1
     * 查询自己的个人信息基本信息
     */
    @GetMapping("/getInfo")
    public Result getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        DanmoInformationBasic danmoInformationBasic = danmoInformationBasicService.getInfo(loginUser.getUserid());
        return Result.success(danmoInformationBasic);
    }

    /**
     * 2
     *
     * @param longs
     * @return
     */
    @PostMapping("/selectUserList")
    public List<DanmoInformationBasic> selectUserList(@RequestBody List<Long> longs)
    {
        List<DanmoInformationBasic> danmoInformationBasicList= danmoInformationBasicService.selectInformationBasicList(longs);
        return  danmoInformationBasicList;
    }

    @GetMapping("/getInfoUser")
    public DanmoInformationBasic getInfoUser(@RequestParam Long userid)
    {
        DanmoInformationBasic danmoInformationBasic = danmoInformationBasicService.getInfo(userid);
        return danmoInformationBasic;
    }

}
